
package com.shenfa.action;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.shenfa.bean.Person;
import com.shenfa.bean.RuleParam;
import com.shenfa.bean.RuleOper;
import com.shenfa.bean.bill.rule.RuleMaster;
import com.shenfa.service.RuleService;
import com.shenfa.tools.SteelDataApp;

import com.shenfa.util.ReadShowFile;
import com.shenfa.util.ResultObject;

/**
 * 规则管理
 * 
 * @author longchensan create by 2011-8-27
 * 
 */
public class RuleAction extends BaseAction implements ServletResponseAware {

	private static final long serialVersionUID = 1L;

	@Autowired
	private RuleService ruleService;
	
	private HttpServletRequest request;

	// 分页，第几条数据
	private int start;
	// 每页显示数据条数
	private int limit;
	// 接收页面数据
	private RuleMaster ruleMaster;
	private RuleParam ruleParamRes;
	private RuleOper ruleOper;
	public RuleOper getRuleOper() {
		return ruleOper;
	}

	public void setRuleOper(RuleOper ruleOper) {
		this.ruleOper = ruleOper;
	}
	private static Logger log=Logger.getLogger(RuleAction.class);
	//接收页面数据 其中的从表的多行数据提交
	private List<RuleParam> ruleparams;
	
	private HttpServletResponse response;
	
	private String errormessage;

	//批量添加从表信息
	public String updateListRuleParam()	{
		log.debug("updateListRuleParam:"+ruleparams.size());
		boolean type = ruleService.updateListRuleParam(ruleparams);
		if(type){
			result = ResultObject.build("1", "添加成功！");
		} else {
			result = ResultObject.build("0", "操作失败！");
		}
		return SUCCESS;
	}
	//接收页面数据 其中的从从表的多行数据提交
	private List<RuleOper> ruleOpers;
	
	
	public List<RuleOper> getRuleOpers() {
		return ruleOpers;
	}

	public void setRuleOpers(List<RuleOper> ruleOpers) {
		this.ruleOpers = ruleOpers;
	}
	

	//批量添加从从表信息
	public String updateListRuleOpers()	{
		log.debug("updateListRuleOpers");
		boolean type=false;
		if(null!=ruleOpers &&ruleOpers.size()>0){
			if(ruleService.delRuleOper(ruleOpers.get(0))){
				type= ruleService.updateListRuleOper(ruleOpers);
			}
			if(type){
				result = ResultObject.build("1", "添加成功！");
			} else {
				result = ResultObject.build("0", "操作失败！");
			}
		}
		return SUCCESS;
	}
	//获取个性化区域规则表的序列号
	public String etRuleNo(){
		String id = ruleService.getRuleNo();
		result = ResultObject.build("1", id);
		return SUCCESS;
	}
	//获取个性化区域规则从表的序列号和丛丛表IDX
	//杨浩宇添加于11.09.09.11：38
	public String etRuleNoOrIdx(){
		int id = ruleService.getRuleNoOrIdx();
		result = ResultObject.build("1", id);
		return SUCCESS;
	}

	public RuleParam getRuleParamRes() {
		return ruleParamRes;
	}

	public void setRuleParamRes(RuleParam ruleParamRes) {
		this.ruleParamRes = ruleParamRes;
	}
	
	public String viewRulePDF(){
		log.debug("viewRulePDF:"+ruleParamRes.toString());
		String readPath=ruleService.rulePDF(ruleParamRes);
		if(null!=readPath && readPath.indexOf(".")!=-1){
			ReadShowFile.read(readPath,response,true);
			return null;
		}else{
			errormessage=readPath;
			return "error";
		}
	}

	public String getErrormessage() {
		return errormessage;
	}

	/**
	 * 查询一个模板区域的规则
	 * @return
	 */
	public String listRuleByArea(){
		log.debug("listRuleByArea:"+ruleMaster.toString());
		if(null==ruleMaster || null==ruleMaster.getAreaNo()||"".equals(ruleMaster.getAreaNo())){
			return ERROR;
		}
		//TODO 测试代码
		List<RuleParam> list=ruleService.listRuleByArea(ruleMaster.getAreaNo());
		result=ResultObject.build("0", list);
		return SUCCESS;
	}
	
	/**
	 * 查询个性化区域规则列表
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String etGridList() {
		log.debug("etGridList:"+ruleMaster.toString()+",start="+start+",limit="+limit);
		List<RuleMaster> list=ruleService.doGridList(ruleMaster, start, limit);
		result = ResultObject.build("0", list);
		result.put("total",ruleService.queryCountRm(ruleMaster));
		return SUCCESS;
	}
	
	/**
	 * 查询审核规则列表
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String etGridListSh() {
		log.debug("etGridList:"+ruleMaster.toString()+",start="+start+",limit="+limit);
		List<RuleMaster> list=ruleService.doGridListSh(ruleMaster, start, limit);
		result = ResultObject.build("0", list);
		result.put("total",ruleService.queryCountRmSh(ruleMaster));
		return SUCCESS;
	}
	
	//判断主表主键是否存在
	public String queryKeyIsValid(){
		int num = ruleService.queryCountRm(ruleMaster);
		if(num == -1) {
			result = ResultObject.build(String.valueOf(9), null);
		} else {
			result = ResultObject.build(String.valueOf(num), null);
		}
		return SUCCESS;
	}
	
	/**
	 * 添加规则区域个性化信息
	 * 
	 * @author yanglong
	 */
	public String add() {
		log.debug("add:"+ruleMaster.toString());
		/**********注释代码唯一性验证已改由前台AJAX验证********/
		request = ServletActionContext.getRequest();
		Person o=(Person) request.getSession().getAttribute("login");
		ruleMaster.setPerson_no(o.getId());
			boolean type = ruleService.addRule(ruleMaster);
			if(type){
				result = ResultObject.build("1", "添加成功！");
			} else {
				result = ResultObject.build("0", "操作失败！");
			}
		return SUCCESS;
	}
	
	/**
	 * 删除规则区域个性化信息
	 * 
	 * @author yanglong
	 */
	public String del() {
		log.debug("del:"+ruleMaster.toString());
		boolean type = ruleService.deleteRule(ruleMaster);
		if(type){
			result = ResultObject.build("1", "删除成功！");
		} else {
			result = ResultObject.build("0", "操作失败！");
		}
		return SUCCESS;
	}
	
	/**
	 * 修改规则区域个性化信息
	 * 
	 * @author yanglong
	 */
	public String upd() {
		log.debug("upd:"+ruleMaster.toString());		
		List<RuleMaster> rlist=validatorRule(ruleMaster);
		if(rlist.size()==0){
			boolean type = ruleService.updateRule(ruleMaster);
			if(type){
				result = ResultObject.build("1", "修改成功！");
			} else {
				result = ResultObject.build("0", "操作失败！");
			}
		}else{
			RuleMaster rm=rlist.get(0);
			if(rm.getRule_no().equals(ruleMaster.getRule_no())){
				boolean type = ruleService.updateRule(ruleMaster);
				if(type){
					result = ResultObject.build("1", "修改成功！");
				} else {
					result = ResultObject.build("0", "操作失败！");
				}
			}else{
				result = ResultObject.build("0", "该个性化规则区域已设置，请重新设置！");
			}
			
		}
		
		return SUCCESS;
	}
	
	/**
	 * 修改个性化规则状态
	 * @return
	 */
	public String updState(){
		boolean type = ruleService.updateRule(ruleMaster);
		if(type){
			result = ResultObject.build("1", "修改成功！");
		} else {
			result = ResultObject.build("0", "操作失败！");
		}
		return SUCCESS;
	}
	/**
	 * 验证个性化规则信息
	 * @return
	 */
	public List<RuleMaster>  validatorRule(RuleMaster ruleInfo){
		log.debug("validatorRule:"+ruleInfo.toString()+",start="+start+",limit="+limit);
		RuleMaster rm=new RuleMaster();
		rm.setRule_type(ruleInfo.getRule_type());		
		rm.setStencil_no(ruleInfo.getStencil_no());
		rm.setAreaNo(ruleInfo.getAreaNo());
		rm.setRule_type(ruleInfo.getRule_type());
		List<RuleMaster> rlist=ruleService.doGridList(rm, start, limit);
		return rlist;
	}
	
	/**
	 * 查询规则的详细信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String listRuleAll() {
		log.debug("listRuleAll:"+ruleMaster.toString());
		List<RuleMaster> list=ruleService.listRuleAll(ruleMaster);
		result = ResultObject.build("0", list);
		result.put("total",ruleService.getCountRmAll(ruleMaster));
		return SUCCESS;
	}
	

	/**
	 * 软删数据
	 * 
	 * @return
	 */
	public String deleteRule() {
		log.debug("deleteRule:"+ruleMaster.toString());
		ruleService.deleteRule(ruleMaster);
		return SUCCESS;
	}



	/**
	 * 格式化月操作
	 * 
	 * @param date
	 * @return
	 */
	public String dateFormat(String date) {
		SteelDataApp app = new SteelDataApp(date);
		date = new SimpleDateFormat("yyyy-MM").format(app.getDate()).toString();
		return date;
	}

	/**
	 * 分页处理，起始条数
	 * 
	 * @param start
	 */
	public void setStart(int start) {
		this.start = Math.abs(start);
	}

	public int getStart() {
		return start;
	}

	/**
	 * 分页处理，每页数据条数
	 * 
	 * @param limit
	 */
	public void setLimit(int limit) {
		this.limit = Math.abs(limit);
	}

	public int getLimit() {
		return limit;
	}

	/**
	 * 页面数据交互
	 * 
	 * @return
	 */
	public RuleMaster getRuleMaster() {
		return ruleMaster;
	}

	public void setRuleMaster(RuleMaster ruleMaster) {
		this.ruleMaster = ruleMaster;
	}
	/**
	 * 查询规则从列表
	 * 
	 * @return
	 */
	public String listRuleParam() {
		log.debug("listRuleParam:"+ruleMaster.toString());
		List<RuleParam> list=ruleService.listRuleParam(ruleMaster);
		result = ResultObject.build("0", list);	
		return SUCCESS;
	}
	/**
	 * 添加规则从表信息
	 * 
	 * @author yanghaoyu
	 */
	public String addRuleParam() {
		log.debug("addRuleParam:"+ruleParamRes.toString());
		boolean type = ruleService.addRuleParam(ruleParamRes);
		if(type){
			result = ResultObject.build("1", "添加成功！");
		} else {
			result = ResultObject.build("0", "操作失败！");
		}
		return SUCCESS;
	}
	/**
	 * 删除规则区域个性化信息  单个记录删除
	 * 
	 * @author yanghaoyu
	 */
	public String delRuleParambyId() {
		log.debug("delRuleParambyId:"+ruleParamRes.toString());
		boolean type = ruleService.deleteRuleParambyId(ruleParamRes);
		if(type){
			result = ResultObject.build("1", "删除成功！");
		} else {
			result = ResultObject.build("0", "操作失败！");
		}
		return SUCCESS;
	}

	/**
	 * 修改规则从表信息
	 * 
	 * @author yanghaoyu
	 */
	public String updateRuleParam() {
		log.debug("updateRuleParam:"+ruleParamRes.toString());
		boolean type = ruleService.updateRuleParambyId(ruleParamRes);
		if(type){
			result = ResultObject.build("1", "修改成功！");
		} else {
			result = ResultObject.build("0", "操作失败！");
		}
		return SUCCESS;
	}
	/**
	 * 查询规则从从列表
	 * 
	 * @return
	 */
	public String listRuleOper() {
		log.debug("listRuleOper:"+ruleParamRes.toString());
		List<RuleOper> list=ruleService.listRuleOper(ruleParamRes);
        result = ResultObject.build("0", list);		
		return SUCCESS;
	}
	/**
	 * 修改规则从从表信息
	 * 
	 * @author yanghaoyu
	 */
	public String updateRuleOper() {
		log.debug("updateRuleOper:"+ruleOper.toString());

			if(ruleService.updateRuleOper(ruleOper)){
				result = ResultObject.build("1", "修改成功！");
			} else {
				result = ResultObject.build("0", "操作失败！");
			}
	
		return SUCCESS;
	}
	/**
	 * 删除规则区丛丛表个性化信息  单个记录删除
	 * 
	 * @author yanghaoyu
	 */
	public String delRuleOperbyIdx() {
		boolean type = ruleService.delRuleOper(ruleOper);
		if(type){
			result = ResultObject.build("1", "删除成功！");
		} else {
			result = ResultObject.build("0", "操作失败！");
		}
		return SUCCESS;
	}
	/**
	 * 添加规则从从表信息
	 * 
	 * @author yanghaoyu
	 */
	public String addRuleOper() {
		log.debug("addRuleOper:"+ruleOper.toString());
		boolean type = ruleService.addRuleOper( ruleOper);
		if(type){
			result = ResultObject.build("1", "添加成功！");
		} else {
			result = ResultObject.build("0", "操作失败！");
		}
		return SUCCESS;
	}
	
	public void setServletResponse(HttpServletResponse arg0) {
		response = arg0;

	}	
	
	public List<RuleParam> getRuleparams() {
		return ruleparams;
	}

	public void setRuleparams(List<RuleParam> ruleparams) {
		this.ruleparams = ruleparams;
	}
}
